﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;

namespace Dtf.Core
{
    public class UIProperty
    {
        static UIProperty()
        {
            Initialization();
        }
        private UIProperty(string name, AutomationProperty property)
        {
            _name = name;
            _property = property;
        }

        static void Add(string name, AutomationProperty property)
        {
            UIProperty uiProp;
            foreach(UIProperty p in _instances)
            {
                if (p.Name == name)
                {
                    return;
                }
            }
            uiProp = new UIProperty(name, property);
            _instances.Add(uiProp);
        }

        static void Initialization()
        {
            if (_instances == null)
            {
                _instances = new List<UIProperty>();
                Add("AcceleratorKey", AutomationElement.AcceleratorKeyProperty);
                Add("AccessKey", AutomationElement.AccessKeyProperty);
                Add("AutomationId", AutomationElement.AutomationIdProperty);
                Add("BoundingRectangle", AutomationElement.BoundingRectangleProperty);
                Add("ClassName", AutomationElement.ClassNameProperty);
                Add("ClickablePoint", AutomationElement.ClickablePointProperty);
                Add("ControlType", AutomationElement.ControlTypeProperty);
                Add("Culture", AutomationElement.CultureProperty);
                Add("FrameworkId", AutomationElement.FrameworkIdProperty);
                Add("HasKeyboardFocus", AutomationElement.HasKeyboardFocusProperty);
                Add("HelpText", AutomationElement.HelpTextProperty);
                Add("IsContentElement", AutomationElement.IsContentElementProperty);
                Add("IsControlElement", AutomationElement.IsControlElementProperty);
                Add("IsDockPatternAvailable", AutomationElement.IsDockPatternAvailableProperty);
                Add("IsEnabled", AutomationElement.IsEnabledProperty);
                Add("IsExpandCollapsePatternAvailable", AutomationElement.IsExpandCollapsePatternAvailableProperty);
                Add("IsGridItemPatternAvailable", AutomationElement.IsGridItemPatternAvailableProperty);
                Add("IsGridPatternAvailable", AutomationElement.IsGridPatternAvailableProperty);
                Add("IsInvokePatternAvailable", AutomationElement.IsInvokePatternAvailableProperty);
                Add("IsKeyboardFocusable", AutomationElement.IsKeyboardFocusableProperty);
                Add("IsMultipleViewPatternAvailable", AutomationElement.IsMultipleViewPatternAvailableProperty);
                Add("IsOffscreen", AutomationElement.IsOffscreenProperty);
                Add("IsPassword", AutomationElement.IsPasswordProperty);
                Add("IsRangeValuePatternAvailable", AutomationElement.IsRangeValuePatternAvailableProperty);
                Add("IsRequiredForForm", AutomationElement.IsRequiredForFormProperty);
                Add("IsScrollItemPatternAvailable", AutomationElement.IsScrollItemPatternAvailableProperty);
                Add("IsScrollPatternAvailable", AutomationElement.IsScrollPatternAvailableProperty);
                Add("IsSelectionItemPatternAvailable", AutomationElement.IsSelectionItemPatternAvailableProperty);
                Add("IsSelectionPatternAvailable", AutomationElement.IsSelectionPatternAvailableProperty);
                Add("IsTableItemPatternAvailable", AutomationElement.IsTableItemPatternAvailableProperty);
                Add("IsTablePatternAvailable", AutomationElement.IsTablePatternAvailableProperty);
                Add("IsTextPatternAvailable", AutomationElement.IsTextPatternAvailableProperty);
                Add("IsTogglePatternAvailable", AutomationElement.IsTogglePatternAvailableProperty);
                Add("IsTransformPatternAvailable", AutomationElement.IsTransformPatternAvailableProperty);
                Add("IsValuePatternAvailable", AutomationElement.IsValuePatternAvailableProperty);
                Add("IsWindowPatternAvailable", AutomationElement.IsWindowPatternAvailableProperty);
                Add("ItemType", AutomationElement.ItemTypeProperty);
                Add("ItemStatus", AutomationElement.ItemStatusProperty);
                Add("LabeledBy", AutomationElement.LabeledByProperty);
                Add("LocalizedControlType", AutomationElement.LocalizedControlTypeProperty);
                Add("Name", AutomationElement.NameProperty);
                Add("NativeWindowHandle", AutomationElement.NativeWindowHandleProperty);
                Add("Orientation", AutomationElement.OrientationProperty);
                Add("ProcessId", AutomationElement.ProcessIdProperty);
                Add("RuntimeId", AutomationElement.RuntimeIdProperty);
                Add("Value.Value", ValuePattern.ValueProperty);
                Add("Value.IsReadOnly", ValuePattern.IsReadOnlyProperty);
                Add("RangeValue.IsReadOnly", RangeValuePattern.IsReadOnlyProperty);
                Add("RangeValue.LargeChange", RangeValuePattern.LargeChangeProperty);
                Add("RangeValue.Maximum", RangeValuePattern.MaximumProperty);
                Add("RangeValue.Minimum", RangeValuePattern.MinimumProperty);
                Add("RangeValue.SmallChange", RangeValuePattern.SmallChangeProperty);
                Add("RangeValue.Value", RangeValuePattern.ValueProperty);
                Add("Scroll.HorizontallyScrollable", ScrollPattern.HorizontallyScrollableProperty);
                Add("Scroll.HorizontalScrollPercent", ScrollPattern.HorizontalScrollPercentProperty);
                Add("Scroll.HorizontalViewSize", ScrollPattern.HorizontalViewSizeProperty);
                Add("Scroll.VerticallyScrollable", ScrollPattern.VerticallyScrollableProperty);
                Add("Scroll.VerticalScrollPercent", ScrollPattern.VerticalScrollPercentProperty);
                Add("Scroll.VerticalViewSize", ScrollPattern.VerticalViewSizeProperty);
                Add("Selection.CanSelectMultiple", SelectionPattern.CanSelectMultipleProperty);
                Add("Selection.IsSelectionRequired", SelectionPattern.IsSelectionRequiredProperty);
                Add("Selection.Selection", SelectionPattern.SelectionProperty);
                Add("Grid.ColumnCount", GridPattern.ColumnCountProperty);
                Add("Grid.RowCount", GridPattern.RowCountProperty);
                Add("GridItem.Column", GridItemPattern.ColumnProperty);
                Add("GridItem.ColumnSpan", GridItemPattern.ColumnSpanProperty);
                Add("GridItem.ContainingGrid", GridItemPattern.ContainingGridProperty);
                Add("GridItem.Row", GridItemPattern.RowProperty);
                Add("GridItem.RowSpan", GridItemPattern.RowSpanProperty);
                Add("Dock.DockPosition", DockPattern.DockPositionProperty);
                Add("ExpandCollapse.ExpandCollapseState", ExpandCollapsePattern.ExpandCollapseStateProperty);
                Add("MultipleView.CurrentView", MultipleViewPattern.CurrentViewProperty);
                Add("MultipleView.SupportedViews", MultipleViewPattern.SupportedViewsProperty);
                Add("Window.CanMaximize", WindowPattern.CanMaximizeProperty);
                Add("Window.CanMinimize", WindowPattern.CanMinimizeProperty);
                Add("Window.WindowInteractionState", WindowPattern.WindowInteractionStateProperty);
                Add("Window.IsModal", WindowPattern.IsModalProperty);
                Add("Window.IsTopmost", WindowPattern.IsTopmostProperty);
                Add("Window.WindowVisualState", WindowPattern.WindowVisualStateProperty);
                Add("SelectionItem.IsSelected", SelectionItemPattern.IsSelectedProperty);
                Add("SelectionItem.SelectionContainer", SelectionItemPattern.SelectionContainerProperty);
                Add("Table.RowHeaders", TablePattern.RowHeadersProperty);
                Add("Table.ColumnHeaders", TablePattern.ColumnHeadersProperty);
                Add("Table.RowOrColumnMajor", TablePattern.RowOrColumnMajorProperty);
                Add("TableItem.RowHeaderItems", TableItemPattern.RowHeaderItemsProperty);
                Add("TableItem.ColumnHeaderItems", TableItemPattern.ColumnHeaderItemsProperty);
                Add("Toggle.ToggleState", TogglePattern.ToggleStateProperty);
                Add("Transform.CanMove", TransformPattern.CanMoveProperty);
                Add("Transform.CanResize", TransformPattern.CanResizeProperty);
                Add("Transform.CanRotate", TransformPattern.CanRotateProperty);

            }
        }

        public static UIProperty Get(string name)
        {
            foreach (UIProperty p in _instances)
            {
                if (p.Name == name)
                {
                    return p;
                }
            }
            return null;
        }

        internal static UIProperty Get(AutomationProperty property)
        {
            foreach (UIProperty p in _instances)
            {
                if (p._property == property)
                {
                    return p;
                }
            }
            return null;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        internal AutomationProperty AutomationProperty
        {
            get
            {
                return _property;
            }
        }

        public override string ToString()
        {
            return _name;
        }

        string _name;
        AutomationProperty _property;
        static List<UIProperty> _instances;
    }
}
